package
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import org.flashbrighton.as3bots.bots.*;
	import org.flashbrighton.as3bots.controllers.GameController;
	import org.flashbrighton.as3bots.display.LoadBotPage;
	import org.flashbrighton.as3bots.events.MatchEndEvent;
	import org.flashbrighton.as3bots.models.GameModel;
	import org.flashbrighton.as3bots.views.GameView;
	import org.flashbrighton.as3bots.views.ui.Message;
	
	
	[SWF(width="800", height="550", frameRate="40", backgroundColor="#333333")]
	public class Main extends Sprite
	{
		[Embed(source = "/../assets/gradius.ttf", fontName = "Gradius", mimeType = "application/x-font-truetype", embedAsCFF = "false")]
		private var GradiusFont:Class;
		
		[Embed(source = "/../assets/Arkitech.ttf", fontName = "Arkitech", mimeType = "application/x-font-truetype", embedAsCFF = "false")]
		private var EvaFont:Class;
		
		private var _model:GameModel;
		private var _controller:GameController;
		private var _view:GameView;
		
		
		protected var botLibrary : Vector.<BaseBot>;
		protected var bots : Vector.<BaseBot>;
		protected var botLoadPage : LoadBotPage;
		
		public function Main()
		{
			addEventListener(Event.ADDED_TO_STAGE, init);

			initBotLoader();
		}
		
		
		protected function initBotLoader():void
		{
			// demo
			botLibrary = Vector.<BaseBot>( [new DummyBot, new Matt2Bot, new JoesBot, new DummyBot, new MattsBot, new JoesBot, new DummyBot, new MattsBot, new JoesBot, new DummyBot] );
			// integration test
			//botLibrary = Vector.<BaseBot>( [new DummyBot, new JoesBot, new DummyBot, new JoesBot, new DummyBot, new JoesBot] );
			// empty 
			//botLibrary = Vector.<BaseBot>( [] );
			bots = new Vector.<BaseBot>;
			botLoadPage = new LoadBotPage( botLibrary, bots );
			botLoadPage.addEventListener( Event.COMPLETE , handleBotLoadPageComplete );
			addChild( botLoadPage );
		}
		
		
		public function addBots():void
		{
			for each( var bot : BaseBot in bots )
			{
				_controller.addBot( bot );
			}
		}
		
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			setStage();
		}
		
		
		private function initGame(e:Event):void
		{
			setGame();
			addBots();
			setupGame();
			initKeyboard();
		}
		
		
		private function setStage():void
		{
			stage.align = StageAlign.TOP_LEFT;
		}
		
		
		private function setGame():void
		{
			_model = new GameModel();
			_view = new GameView(_model);
			_controller = new GameController(_model, _view);
			addEventListener(Event.ENTER_FRAME, _controller.update, false, 0, true);

			_controller.addEventListener(MatchEndEvent.MATCH_END, onMatchEnd);
			addChild(_view);
		}
				
		private function setupGame():void
		{
			_controller.setupGame();
		}
		
		
		private function initKeyboard():void
		{
			stage.focus = this;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			Message.write(null, "* SPACE = START/PAUSE *");
		}
		
		
		private function onKeyDown(k:KeyboardEvent):void
		{
			if(_model==null){
				return;
			}
			switch (k.keyCode)
			{
				case Keyboard.SPACE:
					if (_model.isFinished) 
					{
						resetGame();
					} 
					else if (!_model.started)
					{
						_controller.startGame();
					}
					else if (_model.isPlaying)
					{
						_controller.pauseGame();
					}else{
						//
						_controller.resumeGame();
					}
					break;
			}
			k.stopPropagation();
		}
		
		protected function onMatchEnd(event:MatchEndEvent):void
		{
			botLoadPage.addChampion(event.champion);
		}		

		private function resetGame():void
		{
			//cleanup
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			removeChild(_view);

			botLoadPage.clearArena();
			addChild( botLoadPage );
		}		
		
		protected function handleBotLoadPageComplete( e:Event ):void
		{
			removeChild( botLoadPage );
			initGame( e );
		}
		
	}
	
}
